package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Question;
import com.ruoyi.system.mapper.QuestionMapper;
import com.ruoyi.system.service.IQuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问题Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-02
 */
@Service
public class QuestionServiceImpl implements IQuestionService
{

    private static final Logger log = LoggerFactory.getLogger(QuestionServiceImpl.class);

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询问题
     * 
     * @param id 问题ID
     * @return 问题
     */
    @Override
    public Question selectQuestionById(Long id)
    {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询问题列表
     * 
     * @param question 问题
     * @return 问题
     */
    @Override
    public List<Question> selectQuestionList(Question question)
    {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增问题
     * 
     * @param question 问题
     * @return 结果
     */
    @Override
    public int insertQuestion(Question question)
    {
        return questionMapper.insertQuestion(question);
    }

    /**
     * 修改问题
     * 
     * @param question 问题
     * @return 结果
     */
    @Override
    public int updateQuestion(Question question)
    {
        return questionMapper.updateQuestion(question);
    }

    /**
     * 删除问题对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(String ids)
    {
        return questionMapper.deleteQuestionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除问题信息
     * 
     * @param id 问题ID
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id)
    {
        return questionMapper.deleteQuestionById(id);
    }

    @Override
    public String importQuestion(List<Question> questionList, boolean updateSupport, String operName) {

        if (StringUtils.isNull(questionList) || questionList.size() == 0)
        {
            throw new BusinessException("导入问题数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Question question : questionList)
        {
            try
            {
                // 验证是否存在这个用户
                Question u = questionMapper.selectQuestionById(question.getId());
                if (StringUtils.isNull(u))
                {
                    question.setCreateBy(operName);
                    this.insertQuestion(question);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、问题 " + question.getQuestion() + " 导入成功");
                }
                else if (updateSupport)
                {
                    question.setUpdateBy(operName);
                    this.updateQuestion(question);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、问题 " + question.getQuestion() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、问题 " + question.getQuestion() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、问题 " + question.getQuestion() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();    }


}
